package com.aiziks.android.dicebag;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
//import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
//import android.widget.AdapterView.*;
import android.app.TabActivity;
//import android.widget.TabHost.TabSpec;
import android.view.LayoutInflater;
import java.util.ArrayList;
@SuppressWarnings("unchecked")
public class Dicebag extends TabActivity {
    /** Called when the activity is first created. */
	private ArrayList tvlist, spinlist;
//	private Menu mMenu;
	private TabHost tabHost;
	int DVAL[] = new int[]{100,20,12,10,8,6,4};
	private LinearLayout zx, lx;
	int doptions;
	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tabHost = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("Setup")
                .setContent(R.id.Tab1));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("Output")
                .setContent(R.id.Tab2));
        SetUpInterface();
    }
	   public boolean SetUpInterface()
	   {
	    	doptions = DVAL.length;
	    	zx = (LinearLayout)findViewById(R.id.ll1);
	    	lx = (LinearLayout)findViewById(R.id.ll2);
	        tvlist = new ArrayList();
	        spinlist = new ArrayList();
	        for (int i = 0; i < doptions; i++){
	        	TextView tv = new TextView(this);
	        	tvlist.add(tv);
	        	Spinner spinme = new Spinner(this);
	            ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,new String[] {"0","1","2","3","4","5","6","7","8","9"});
	            spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	            spinme.setAdapter(spinnerArrayAdapter);
	            spinme.setSaveEnabled(true);
	        	spinlist.add(spinme);
	        	tv.setText("d" + Integer.toString(DVAL[i]) + ":");
	        	zx.addView(tv,layoutParams);
	        	zx.addView(spinme,layoutParams);
	        }
		   return true;
	   }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Hold on to this
//        mMenu = menu;
        
        // Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.dicebagmenu, menu);
        
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // For "Title only": Examples of matching an ID with one assigned in
            //                   the XML
            case R.id.roll:
                //Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show();
            	int dposition[] = new int[doptions];
            	for (int i = 0; i < doptions; i++){
            		dposition[i] = ((AdapterView<SpinnerAdapter>) spinlist.get(i)).getSelectedItemPosition();
            	}
            	lx.removeAllViews();
            	TextView currentroll, DieHeader;
            	ArrayList RollerDice = new ArrayList();
            	for (int i = 0; i < doptions; i++){
            		RollDice rnum = new RollDice(DVAL[i]);
            		RollerDice.add(rnum);
            	}
            	for (int z = 0; z < doptions; z++){
            		for (int q = 0; q < dposition[z]; q++){
            			if (q == 0){
                			DieHeader = new TextView(this);
                			DieHeader.setText("Rolls for D" + DVAL[z]);
                			lx.addView(DieHeader,layoutParams);
            			}
                		currentroll = new TextView(this);
                		((RollDice) RollerDice.get(z)).roll();
                		currentroll.setText(Integer.toString(((RollDice) RollerDice.get(z)).getFaceValue()));
                		lx.addView(currentroll,layoutParams);
            		}
            	}
            	tabHost.setCurrentTab(1);            	
                return true;
            case R.id.clear:
            	lx.removeAllViews();
            	zx.removeAllViews();
            	SetUpInterface();
            	tabHost.setCurrentTab(0);
            	return true;
            default:
                // Don't toast text when a submenu is clicked
                if (!item.hasSubMenu()) {
                    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                break;
        }
        
        return false;
    }

}